package org.irri.smta.ui.bean;

import javax.faces.bean.ApplicationScoped;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.irri.smta.config.impl.Configuration;

@Named("resourceConfigBean")
@ApplicationScoped
public class ResourceConfigBean extends Configuration {
	
	private static final long serialVersionUID = 1L;
	
	private static final String DEFAULT_URL_HTTP_SCHEME = "http";
	
	public String buildUrl(String scheme, String host, String port, String path) {
		StringBuffer url = new StringBuffer();
		if (scheme != null && StringUtils.isNotEmpty(scheme.trim())) {
			url.append(scheme.trim());
			url.append("://");
		}
		if (host != null && StringUtils.isNotEmpty(host.trim())) {
			if (!StringUtils.endsWith(url.toString(), "/")) {
				url.append("/");
			}
			url.append(host.trim());
		}
		if (port != null && StringUtils.isNotEmpty(port.trim())) {
			url.append(":");
			url.append(port.trim());
		}
		if (path != null && StringUtils.isNotEmpty(path.trim())) {
			if (!StringUtils.endsWith(url.toString(), "/")) {
				url.append("/");
			}
			url.append(path.trim());
		}
		return url.toString();
	}
	
	public String buildUrl(String scheme, String host, String port) {
		return buildUrl(scheme, host, port, null);
	}
	
	public String getUrlTreatyScheme() {
		return getString("/application/url/treaty/scheme", DEFAULT_URL_HTTP_SCHEME);
	}

	public String getUrlTreatyHost() {
		return getString("/application/url/treaty/host", "");
	}

	public String getUrlTreatyPort() {
		return getString("/application/url/treaty/port", "");
	}

	public String getUrlTreatyPath() {
		return getString("/application/url/treaty/path", "");
	}

	public String getUrlTreatyQueryString() {
		return getString("/application/url/treaty/querystring", "");
	}

	public String getUrlTreatyUsername() {
		return getString("/application/url/treaty/username", "");
	}

	public String getUrlTreatyPassword() {
		return getString("/application/url/treaty/password", "");
	}
	
	public Boolean getUrlTreatyIsReportingOn() {
		return getBoolean("/application/url/treaty/isReportingOn", false);
	}
	
	public String getUrlSmtaWebsite() {
		return getString("/application/url/smtawebsite", "");
	}

	public String getOpenOfficeDirectory() {
		return getString("/application/openoffice/directory");
	}
	
	public String getSmtaTemplateDirectory() {
		return getString("/application/smtaTemplate/directory");
	}

	public String getSmtaTemplateFilename() {
		return getString("/application/smtaTemplate/filename");
	}

	public String getMtaTemplateDirectory() {
		return getString("/application/mtaTemplate/directory");
	}

	public String getMtaTemplateFilename() {
		return getString("/application/mtaTemplate/filename");
	}

	public String getGeneratedDirectory() {
		return getString("/application/generated/directory");
	}

	public String getIrriAuthorizedOfficialDetails() {
		return getString("/application/document/irriAuthorizedOfficialDetails");
	}

}
